package com.utils.demo;

import com.utils.bean.SupremeLotteryDetailCfg;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;

/**
 * @author Devin
 * @program my-java
 * @description
 * @date 2024-09-06 17:55
 **/
public class ActivityUnitTest {
    public static void main(String[] args) {
        test1();

    }
    public static void test1() {
        HashMap<Integer, SupremeLotteryDetailCfg> lotteryDetailCfgMap = new HashMap<>();
        lotteryDetailCfgMap.put(1, new SupremeLotteryDetailCfg(1,1, "至臻角色—江小鱼", 2));
        lotteryDetailCfgMap.put(2, new SupremeLotteryDetailCfg(2,1, "江小鱼头像框", 4000));
        lotteryDetailCfgMap.put(3, new SupremeLotteryDetailCfg(3,1, "江小鱼静态头像", 5000));
        lotteryDetailCfgMap.put(4, new SupremeLotteryDetailCfg(4,1, "江小鱼聊天气泡框", 5000));
        lotteryDetailCfgMap.put(5, new SupremeLotteryDetailCfg(5,1, "聊天表情（吃我一拳）", 2500));
        lotteryDetailCfgMap.put(6, new SupremeLotteryDetailCfg(6,1, "聊天表情（坏笑）", 2500));
        lotteryDetailCfgMap.put(7, new SupremeLotteryDetailCfg(7,1, "江小鱼动态头像", 2000));
        lotteryDetailCfgMap.put(8, new SupremeLotteryDetailCfg(8,1, "一掷千金斩特效", 2000));
        lotteryDetailCfgMap.put(9, new SupremeLotteryDetailCfg(9,1, "互动表情—收币宝袋", 2000));
        lotteryDetailCfgMap.put(10, new SupremeLotteryDetailCfg(10,0, "神兵·雷神锤", 2000));
        lotteryDetailCfgMap.put(11, new SupremeLotteryDetailCfg(11,0, "神兵·龙胆亮银枪", 2000));
        lotteryDetailCfgMap.put(12, new SupremeLotteryDetailCfg(12,0, "至臻碎片*1", 25000));
        lotteryDetailCfgMap.put(13, new SupremeLotteryDetailCfg(13,0, "至臻碎片*3", 40998));
        lotteryDetailCfgMap.put(14, new SupremeLotteryDetailCfg(14,0, "至臻碎片*6", 1800));
        lotteryDetailCfgMap.put(15, new SupremeLotteryDetailCfg(15,0, "至臻碎片*10", 3000));
        lotteryDetailCfgMap.put(16, new SupremeLotteryDetailCfg(16,0, "至臻碎片*66", 200));


        HashSet<Integer> wonIdSet = new HashSet<>();

        int xiaoyuer = 0;
        int chuiziNum = 0;
        int liangyinQiang = 0;
        int times = 3000;
        for (int i = 0; i < times; i++) {
            // 总权重
            int totalWeight = 0;
            // 过滤不能重复抽中的奖品
            HashMap<Integer, SupremeLotteryDetailCfg> awardCfgMap = new HashMap<>(lotteryDetailCfgMap);
            for (SupremeLotteryDetailCfg lotteryCfg : lotteryDetailCfgMap.values()) {
                if (lotteryCfg.getCanReDraw() > 0 && wonIdSet.contains(lotteryCfg.getId())) {
                    awardCfgMap.remove(lotteryCfg.getId());
                    continue;
                }
                totalWeight = lotteryCfg.getWeight() + totalWeight;
            }
//            int totalWeight = awardCfgMap.values().stream()
//                    .mapToInt(SupremeLotteryDetailCfg::getWeight)
//                    .sum();


            Random rand = new Random();
            // 抽奖
            if (totalWeight > 0) {
                int randomWeight = rand.nextInt(totalWeight);
                int nowWeight = 0;
                for (Map.Entry<Integer, SupremeLotteryDetailCfg> award : awardCfgMap.entrySet()) {
                    int weight = award.getValue().getWeight() + nowWeight;
                    if (weight > randomWeight) {
                        int deliverAwardId = award.getKey();
                        wonIdSet.add(deliverAwardId);
                        if (deliverAwardId == 10) {
                            chuiziNum++;
                        }
                        if (deliverAwardId == 11) {
                            liangyinQiang++;
                        }
                        if (deliverAwardId == 1) {
                            xiaoyuer++;
                        }
                        break;
                    } else {
                        nowWeight = weight;
                    }
                }
            }
        }

        System.out.println(times+"抽中，小鱼儿出现次数：" + xiaoyuer + ",概率为：" + (double) xiaoyuer / times);
        System.out.println(times+"抽中，雷神锤出现次数：" + chuiziNum + ",概率为：" + (double) chuiziNum / times);
        System.out.println(times+"抽中，亮银枪出现次数：" + liangyinQiang + ",概率为：" + (double) liangyinQiang / times);

    }
}
